package com.cssy.xxkj.logic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.dao.LogicWarehouseDetailsDAO;
import com.cssy.xxkj.logic.entity.LogicWarehouseDetailsDO;
import com.cssy.xxkj.logic.entity.vo.LogicPurchaseWareSumVO;
import com.cssy.xxkj.logic.entity.vo.LogicWarehouseDetailsVO;
import com.cssy.xxkj.logic.service.ILogicWarehouseDetailsService;
import com.cssy.yd.logic.vo.LogicWhAffVO;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.ExcelUtil;

@Service("logicWarehouseDetailsService")
public class LogicWarehouseDetailsServiceImpl implements ILogicWarehouseDetailsService{
	
	@Resource(name = "logicWarehouseDetailsDAO")
	private LogicWarehouseDetailsDAO logicWarehouseDetailsDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		if(StringUtils.isEmpty(scs.getOrderBy())){
			scs.setOrderBy("bizCreatorDate asc");
		}
		List<LogicWarehouseDetailsDO>  list=logicWarehouseDetailsDAO.find(scs);
		rs.setRecordCount(logicWarehouseDetailsDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicWarehouseDetailsDAO.deleteLogicWarehouseDetailsDOByPrimaryKey(id);
		return i;
	}

	@Override
	public Integer update(LogicWarehouseDetailsDO t, User user) throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		return logicWarehouseDetailsDAO.updateLogicWarehouseDetailsDO(t);
	}

	@Override
	public LogicWarehouseDetailsDO save(LogicWarehouseDetailsDO t, User user) throws SyBaseException {
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(new Date());
		logicWarehouseDetailsDAO.insertLogicWarehouseDetailsDO(t);
		return t;
	}

	@Override
	public LogicWarehouseDetailsDO findListById(String id) throws SyBaseException {
		return logicWarehouseDetailsDAO.findLogicWarehouseDetailsDOByPrimaryKey(id);
	}
	
	@Override
	public SearchResult findListByExampleForDetails(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
//		List<LogicWhAffVO>  list=logicWhAffDAO.findListBySearchConForDetails(scs);
//		rs.setRecordCount(logicWhAffDAO.findListBySearchConForDetailsCount(scs));
//		rs.setResults(list);
		return rs;
	}
	
	@Override
	public void exportExcel(SearchConditions scs, HttpServletResponse response, String text) throws SyBaseException {
		
		SearchResult result = this.findListByExampleForDetails(scs);
		List<LogicWhAffVO> list = result.getResults();
		for (LogicWhAffVO lwv : list){
			if ("1".equals(lwv.getStatus())){
				lwv.setStatus("涓�瓑");
			}
			if ("2".equals(lwv.getStatus())){
				lwv.setStatus("娆″搧");
			}
			if ("3".equals(lwv.getStatus())){
				lwv.setStatus("鏃у搧");
			}
			
			if("OC".equals(lwv.getBillType())){
				lwv.setBillType("鍏朵粬鍏ュ簱");
			}else if("QC".equals(lwv.getBillType())){
				lwv.setBillType("鍏朵粬鍑哄簱");
			}else if("WC".equals(lwv.getBillType())){
				lwv.setBillType("棰嗘枡鍑哄簱");
			}else if("WT".equals(lwv.getBillType())){
				lwv.setBillType("棰嗘枡閫�枡");
			}else if("ST".equals(lwv.getBillType())){
				lwv.setBillType("璋冩嫧鍙戣捣");
			}else if("RC".equals(lwv.getBillType())){
				lwv.setBillType("璋冩嫧鎺ユ敹");
			}else if("CR".equals(lwv.getBillType())){
				lwv.setBillType("閲囪喘鍏ュ簱");
			}else if("CT".equals(lwv.getBillType())){
				lwv.setBillType("閲囪喘閫�揣");
			}else if("PB".equals(lwv.getBillType())){
				lwv.setBillType("搴撳瓨鐩樼偣");
			}
		}
		ExcelUtil.toExcel(list, "鍗曟嵁绫诲瀷:billType鈹嬪崟鎹紪鐮�billCode鈹嬫墍灞炲尯鍩�area鈹嬬墿鏂欑被鍨�maType鈹嬬墿鏂欏悕绉�name鈹嬬墿鏂欑紪鐮�code鈹嬪瀷鍙�spec鈹嬪崟浣�unit鈹嬩粨搴�whName鈹嬩粨浣�hubName鈹嬬墿鍝佺姸鎬�status鈹嬫暟閲�quantity鈹嬪缓鍗曟椂闂�dctime",text,response);
	}

	@Override
	public SearchResult findAll(Map map) throws SyBaseException {
		SearchResult rs=new SearchResult();
//		List<LogicWhAffVO>  list=logicWhAffDAO.findAll(map);
////		if(list.size()>0){
////			for(int i=0;i<list.size();i++){
////				SearchConditions scs = new SearchConditions();
////				scs.addCondition("whcode", list.get(i).getWhCode());
////				scs.addCondition("hubcode", list.get(i).getHubCode());
////				scs.addCondition("code", list.get(i).getCode());
////				List<LogicWhAffstockDO>  list1=logicWhAffstockDAO.find(scs);
////				if(list1.size()>0){
////					list.get(i).setSumquantity(list1.get(0).getYdengquantity()-list.get(i).getRkquantity()-list.get(i).getCkquantity());
////					list.get(i).setJrkc(list1.get(0).getYdengquantity());
////				}
////			}
////		}
//		rs.setRecordCount(logicWhAffDAO.countAll(map));
//		rs.setResults(list);
		return rs;
	}

	@Override
	public SearchResult findAll1(Map map) throws SyBaseException {
		SearchResult rs=new SearchResult();
//		List<LogicWhAffVO1>  list=logicWhAffDAO.findAll1(map);
//		rs.setResults(list);
		return rs;
	}

	@Override
	public SearchResult findFinance(Map map) throws SyBaseException {
		SearchResult rs=new SearchResult();
//		List<LogicWhAffStockFinanceDO>  list=logicWhAffDAO.findFinance(map);
//		rs.setRecordCount(logicWhAffDAO.countFinance(map));
//		rs.setResults(list);
		return rs;
	}

	@Override
	public LogicWarehouseDetailsDO findInOutBycode(Map map)
			throws SyBaseException {
//		LogicWhAffStockFinanceDO lwfd = logicWhAffDAO.findInOutBycode(map);
//		return lwfd;
		return null;
	}

	@Override
	public LogicWarehouseDetailsDO findCKBycode(Map map)
			throws SyBaseException {
//		LogicWhAffStockFinanceDO lwfd = logicWhAffDAO.findCKBycode(map);
//		return lwfd;
		return null;
	}
//	@Override
//	public List<LogicWhAffVO> findStarQty(Map map) throws SyBaseException{
//		List<LogicWhAffVO> list = logicWhAffDAO.findStarQty(map);
//		return list;
//	}
//	@Override
//	public List<LogicWhAffVO> findEndQty(Map map) throws SyBaseException{
//		List<LogicWhAffVO> list = logicWhAffDAO.findEndQty(map);
//		return list;
//	}
//	@Override
//	public List<LogicWhAffVO> findInOutQty(Map map) throws SyBaseException{
//		List<LogicWhAffVO> list = logicWhAffDAO.findInOutQty(map);
//		return list;
//	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SearchResult findDaliyReport(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWarehouseDetailsVO>  list=logicWarehouseDetailsDAO.findDaliyReport(scs);
		rs.setRecordCount(logicWarehouseDetailsDAO.findDaliyReportCount(scs));
		rs.setResults(list);
		return rs;
	}
	
	//采购入库汇总统计
	@Override
	public SearchResult findQueryPcswToSummary(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicPurchaseWareSumVO> list = logicWarehouseDetailsDAO.findQueryPcswToSummary(scs);
		rs.setRecordCount(logicWarehouseDetailsDAO.countQueryPcswToSummary(scs));
		rs.setResults(list);
		return rs;
	}
	
}
